package com.ruoyi.system.baidu;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.MatchRequest;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

@Component
public class BaiduClient {
    public static final String APP_ID = "31737828";
    public static final String API_KEY = "mq38T2mPMMH8rNfPgBBISEAH";
    public static final String SECRET_KEY = "PDrevpGN3AyYFb9U8nKRu0YWOltfso7O";

    // 初始化一个AipFace
    private static AipFace CLIENT = new AipFace(APP_ID, API_KEY, SECRET_KEY);

    public boolean match(String base641, String base642){
        MatchRequest req1 = new MatchRequest(base641, "BASE64");
        MatchRequest req2 = new MatchRequest(base642, "BASE64");
        ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
        requests.add(req1);
        requests.add(req2);

        JSONObject res = CLIENT.match(requests);
        System.out.println(res.toString(2));
        try{
            double score = res.getJSONObject("result").getDouble("score");
            //对比结果置信度超过90%认为是同一个人
            return score > 90.0D;
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return false;
    }
}
